<template>
    <div>
        <h1>人员列表</h1>
        <h2>Count组件求和为：{{ countSum }}</h2>
        <h3>列表中第一个人的姓名:{{ firstName }}</h3>
        <input type="text" placeholder="请输入名字" v-model="name">
        <button @click="addPerson">添加</button>
        <button @click="addWang">添加一个姓王的人</button>
        <button @click="getMsgFromServer">向服务器发送请求</button>
        <ul>
            <li v-for="p in personList" :key="p.id"> {{ p.name }}</li>
        </ul>
    </div>

</template>

<script>
import {nanoid} from 'nanoid'
export default {
    name:'Person',
    data(){
        return{
            name:''
        }
    },
    computed:{
        personList(){
            return this.$store.state.personList.personList
        },
        countSum(){
            return this.$store.state.sum.sum
        },
        firstName(){
            return this.$store.getters['personList/firstPersonName']
        }
    },
    methods:{
        addPerson(){
            const personObj = {id:nanoid(), name:this.name}
            this.$store.commit('personList/ADD_PERSON', personObj)
        },
        addWang(){
            const personObj = {id:nanoid(), name:this.name}
            this.$store.dispatch('personList/addPersonWang', personObj)
        },
        getMsgFromServer(){
            this.$store.dispatch('personList/getMsgFromServer')
        }
    },
    mounted(){
        console.log(this.$store)
    }
}
</script>

<style>

</style>